﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Address_Book.BLL
{
    //导入外部文件好友信息
    public class Import
    {
        public int ImpotrQQCSV(StreamReader sr)
        {
            //导入QQ通信录CSV文件。
            //其中CSV文件结构为：
            //"姓","名","QQ","住宅地址 街道","住宅地址 邮政编码","住宅手机","住宅电话","单位","商务手机","商务电话","备注","电子邮件","电话","移动电话","移动电话 2",

            Address_Book.Model.FriendInfo finfo = new Address_Book.Model.FriendInfo();
            //记录每次读取的一行记录
            string strLine = "";
            //记录每行记录中的各字段内容
            string[] aryLine;
            //记录添加成功行数
            int successLine = 0;
            //标示是否是读取的第一行
            bool IsFirst = true;
            
            //逐行读取CSV中的数据
            while ((strLine = sr.ReadLine()) != null)
            {
                aryLine = strLine.Split(',');
                for (int i = 0; i < aryLine.Length; i++)//处理各字段的""双引号
                {
                    aryLine[i] = aryLine[i].Replace("\"", "");
                }
                if (IsFirst == true)//处理第一行字段名
                {
                    IsFirst = false;
                }
                else
                {
                    //提取各字段信息
                    finfo.FName = aryLine[0] + aryLine[1];
                    finfo.FQQ =aryLine[2];
                    finfo.FAddress = aryLine[3];
                    finfo.FHomeTel = aryLine[6];
                    finfo.FNote = aryLine[10];
                    finfo.FE_Mail = aryLine[11];
                    finfo.FTel1 = aryLine[13];
                    finfo.FTel2 = aryLine[14];

                    finfo.FBirthday = "";
                    finfo.FSex = "";
                    finfo.FLelever = "";                   
                    
                    //添加信息到数据库
                    string strOperate = "p_AddFriends";
                    Address_Book.DAL.Friends exct = new Address_Book.DAL.Friends();
                    if (exct.UpData(finfo, strOperate))//记录添加条数
                    {
                        successLine += 1;
                    }
                }
            }
            return successLine;
        }

        public int ImpotrFetionCSV(StreamReader sr)
        {
            //导入Fetion通信录CSV文件。
            //其中CSV文件结构为：
            //FetionId,MobileNo,Name,Nickname

            Address_Book.Model.FriendInfo finfo = new Address_Book.Model.FriendInfo();
            //记录每次读取的一行记录
            string strLine = "";
            //记录每行记录中的各字段内容
            string[] aryLine;
            //记录添加成功行数
            int successLine = 0;
            //标示是否是读取的第一行
            bool IsFirst = true;

            //逐行读取CSV中的数据
            while ((strLine = sr.ReadLine()) != null)
            {
                aryLine = strLine.Split(',');
                if (IsFirst == true)//处理第一行字段名
                {
                    IsFirst = false;
                }
                else
                {
                    //提取各字段信息
                    finfo.FName = aryLine[3];
                    finfo.FQQ = "";
                    finfo.FAddress = "";
                    finfo.FHomeTel = "";
                    finfo.FNote = "";
                    finfo.FE_Mail = "";
                    finfo.FTel1 = aryLine[1];
                    finfo.FTel2 = "";

                    finfo.FBirthday = "";
                    finfo.FSex = "";
                    finfo.FLelever = "";

                    //添加信息到数据库
                    string strOperate = "p_AddFriends";
                    Address_Book.DAL.Friends exct = new Address_Book.DAL.Friends();
                    if (exct.UpData(finfo, strOperate))//记录添加条数
                    {
                        successLine += 1;
                    }
                }
            }
            return successLine;
        }

        public int ImpotrAddressBookCSV(StreamReader sr)
        {
            //导入AddressBoo通信录CSV文件。
            //其中CSV文件结构为：
            //好友姓名,性别,生日,手机1,手机2,固话,QQ号码,电子邮箱,家庭地址,好友级别,备注

            Address_Book.Model.FriendInfo finfo = new Address_Book.Model.FriendInfo();
            //记录每次读取的一行记录
            string strLine = "";
            //记录每行记录中的各字段内容
            string[] aryLine;
            //记录添加成功行数
            int successLine = 0;
            //标示是否是读取的第一行
            bool IsFirst = true;

            //逐行读取CSV中的数据
            while ((strLine = sr.ReadLine()) != null)
            {
                aryLine = strLine.Split(',');
                if (IsFirst == true)//处理第一行字段名
                {
                    IsFirst = false;
                }
                else
                {
                    //提取各字段信息
                    finfo.FName = aryLine[0];
                    finfo.FSex = aryLine[1];
                    finfo.FBirthday = aryLine[2];
                    finfo.FTel1 = aryLine[3];
                    finfo.FTel2 = aryLine[4];
                    finfo.FHomeTel = aryLine[5];
                    finfo.FQQ = aryLine[6];
                    finfo.FE_Mail = aryLine[7];

                    finfo.FAddress = aryLine[8];
                    finfo.FLelever = aryLine[9];
                    finfo.FNote = aryLine[10];

                    //添加信息到数据库
                    string strOperate = "p_AddFriends";
                    Address_Book.DAL.Friends exct = new Address_Book.DAL.Friends();
                    if (exct.UpData(finfo, strOperate))//记录添加条数
                    {
                        successLine += 1;
                    }
                }
            }
            return successLine;
        }
    }
}
